<%-- 
    Document   : Stock Barang
    Created on : 26 Nov 13, 19:01:17
    Author     : PUTIK ALWENA
--%>

<%@page import="java.sql.ResultSet"%>
<%@page import="java.util.Set"%>
<%@page import="Model.Pengiriman"%>
<%@page import="java.util.List"%>
<%@page import="java.util.logging.Logger"%>
<%@page import="java.util.logging.Level"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.Statement"%>
<%@page import="Model.DatabaseConnect"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Halaman Kirim Barang Control</title>
    </head>
    <body background="background.jpg">
        <form action="KirimBarang.jsp" method="post" name="kirim">
            <%
                Model.Pengiriman pengiriman = (Model.Pengiriman) session.getAttribute("kirim");
                if (pengiriman == null) {
                    pengiriman = new Model.Pengiriman();
                    session.setAttribute("kirim", pengiriman);
                                       }
                pengiriman.setKodeToko((String) request.getParameter("kodetoko"));
                pengiriman.setKodeBarang((String) request.getParameter("kodebarang"));
                pengiriman.setNamaBarang((String) request.getParameter("namabarang"));
                pengiriman.setJenisBarang((String) request.getParameter("jenisbarang"));
                pengiriman.setHargaBarang(Double.parseDouble((request.getParameter("hargabarang"))));
                pengiriman.setJumlahBarang(Integer.parseInt((request.getParameter("jumlahbarang"))));
                pengiriman.setId_pengiriman((String) request.getParameter("idkirim"));
                pengiriman.setTgl_pengiriman((String) request.getParameter("tgl_kirim"));

                Connection conn = null;
                String pilih = request.getParameter("kodetoko");
                
                if (pilih.matches("none")) {
                    out.println("Silahkan Pilih Toko Terlebih dahulu");
            %> 
            <p><b><a href="KirimBarang.jsp">Kembali</a> <b></p>
                        <%
                        } 
                if (pilih.matches("ck02")) {
                            try {
                                conn = DatabaseConnect.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "mhs125314075", "mhs125314075");
                                //conn = DatabaseConnect.open("jdbc:oracle:thin:@localhost:1521:XE", "reynusa", "reynusa");
                                Statement statement = conn.createStatement();
                                String sql = "insert into kirimbarang values ('"
                                        + pengiriman.getKodeToko() + "','"
                                        + pengiriman.getKodeBarang() + "','"
                                        + pengiriman.getNamaBarang() + "','"
                                        + pengiriman.getJenisBarang() + "','"
                                        + pengiriman.getHargaBarang() + "','"
                                        + pengiriman.getJumlahBarang() + "','"
                                        + pengiriman.getId_pengiriman() + "','"
                                        + pengiriman.getTgl_pengiriman() + "')";
                                String sql3 = "update stoka set jumlahbarang=jumlahbarang+" + pengiriman.getJumlahBarang() + " where kodebarang='" + pengiriman.getKodeBarang() + "'";
                                String sql4 = "update stok set jumlahbarang=jumlahbarang-" + pengiriman.getJumlahBarang() + " where kodebarang='" + pengiriman.getKodeBarang() + "'";

                                statement.executeQuery(sql);
                                statement.executeQuery(sql3);
                                statement.executeQuery(sql4);
                                conn.close();

                            } catch (SQLException ex) {
                                Logger.getLogger(DatabaseConnect.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        } 
                if (pilih.matches("ck03")) {
                            try {
                                conn = DatabaseConnect.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "mhs125314075", "mhs125314075");
                                //conn = DatabaseConnect.open("jdbc:oracle:thin:@localhost:1521:XE", "reynusa", "reynusa");
                                Statement statement = conn.createStatement();
                                String sql = "insert into kirimbarang values ('"
                                        + pengiriman.getKodeToko() + "','"
                                        + pengiriman.getKodeBarang() + "','"
                                        + pengiriman.getNamaBarang() + "','"
                                        + pengiriman.getJenisBarang() + "','"
                                        + pengiriman.getHargaBarang() + "','"
                                        + pengiriman.getJumlahBarang() + "','"
                                        + pengiriman.getId_pengiriman() + "','"
                                        + pengiriman.getTgl_pengiriman() + "')";
                                String sql2 = "insert into stokb values ('"
                                        + pengiriman.getKodeBarang() + "','"
                                        + pengiriman.getNamaBarang() + "','"
                                        + pengiriman.getJenisBarang() + "','"
                                        + pengiriman.getHargaBarang() + "')";
                                String sql3 = "update stokb set jumlahbarang=jumlahbarang+" + pengiriman.getJumlahBarang() + " where kodebarang='" + pengiriman.getKodeBarang() + "'";
                                String sql4 = "update stok set jumlahbarang=jumlahbarang-" + pengiriman.getJumlahBarang() + " where kodebarang='" + pengiriman.getKodeBarang() + "'";

                                statement.executeQuery(sql);
                                statement.executeQuery(sql2);
                                statement.executeQuery(sql3);
                                statement.executeQuery(sql4);
                                conn.close();
                            } catch (SQLException ex) {
                                Logger.getLogger(DatabaseConnect.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                if (pilih.matches("ck04")) {
                            try {
                                conn = DatabaseConnect.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "mhs125314075", "mhs125314075");
                                //conn = DatabaseConnect.open("jdbc:oracle:thin:@localhost:1521:XE", "reynusa", "reynusa");
                                Statement statement = conn.createStatement();
                                String sql = "insert into kirimbarang values ('"
                                        + pengiriman.getKodeToko() + "','"
                                        + pengiriman.getKodeBarang() + "','"
                                        + pengiriman.getNamaBarang() + "','"
                                        + pengiriman.getJenisBarang() + "','"
                                        + pengiriman.getHargaBarang() + "','"
                                        + pengiriman.getJumlahBarang() + "','"
                                        + pengiriman.getId_pengiriman() + "','"
                                        + pengiriman.getTgl_pengiriman() + "')";
                                String sql2 = "insert into stokc values ('"
                                        + pengiriman.getKodeBarang() + "','"
                                        + pengiriman.getNamaBarang() + "','"
                                        + pengiriman.getJenisBarang() + "','"
                                        + pengiriman.getHargaBarang() + "')";

                                String sql3 = "update stokc set jumlahbarang=jumlahbarang+" + pengiriman.getJumlahBarang() + " where kodebarang='" + pengiriman.getKodeBarang() + "'";
                                String sql4 = "update stok set jumlahbarang=jumlahbarang-" + pengiriman.getJumlahBarang() + " where kodebarang='" + pengiriman.getKodeBarang() + "'";

                                statement.executeQuery(sql);
                                statement.executeQuery(sql2);
                                statement.executeQuery(sql3);
                                statement.executeQuery(sql4);
                                conn.close();
                            } catch (SQLException ex) {
                                Logger.getLogger(DatabaseConnect.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        } else {
                            out.println(" ");
                        %>
                        <p><a href="KirimBarang.jsp">Kembali</a> </p>
                        <%
                            }
                        %>
                        </form>
                        <table width="1009" border="0">
                            <tr>
                                <td width="406" height="28" align="center" valign="middle"><h2><a href="PesananBarang.jsp">Daftar Pesanan</a></h2></td>
                                <td width="286" align="center" valign="middle"><h2><a href="StokBarang.jsp">Stock Barang</a></h2></td>
                                <td width="295" align="center" valign="middle"><h2><a href="PesanBarang.jsp">Pesan Barang</a></h2></td>
                                <td width="295" align="center" valign="middle"><h2><a href="KirimBarang.jsp">Pengiriman Barang</a></h2></td>
                                <td width="295" align="center" valign="middle"><h2><a href="TerimaBarang.jsp">Terima Barang</a></h2></td> 
                                <td width="295" align="center" valign="middle"><h2> <a href="Login.jsp">Logout</a></h2></td>
                            </tr>
                            <%
                                Pengiriman kirim = new Pengiriman();
                                String idkirim = request.getParameter("idkirim");
                                String tgl_kirim = request.getParameter("tgl_kirim");
                                List<Pengiriman> listkirim = kirim.lihatpengiriman(idkirim, tgl_kirim);
                            %>
                            <tr> 
            
                            <table align="center" width="800" height="75" border="0">
                                                    <b><a>Tabel Kirim</a></b>
                                <th bgcolor="#999999" width="100"><div align="center">Kode Toko</div> </th>
                                <th bgcolor="#999999" width="150"><div align="center">Kode Barang</div> </th>
                                <th bgcolor="#999999" width="250"><div align="center">Nama Barang</div> </th>
                                <th bgcolor="#999999" width="200"><div align="center">Jenis Barang</div></th>
                                <th bgcolor="#999999" width="150"><div align="center">Harga Barang</div></th>
                                <th bgcolor="#999999" width="100"><div align="center">Jumlah</div></th>
                                <th bgcolor="#999999" width="100"><div align="center">ID Kirim </div></th>
                                <th bgcolor="#999999" width="200"><div align="center">Tgl Kirim</div> </th>
                                </tr>
                                <tr>
                                    <%

                                        for (Pengiriman temp : listkirim) {
                                    %>
                                    <td bgcolor="#FFFFFF"><div align="center"><%=temp.getKodeToko()%></div></td>
                                    <td bgcolor="#FFFFFF"><div align="center"><%=temp.getKodeBarang()%></div></td>
                                    <td bgcolor="#FFFFFF"><div align="center"><%=temp.getNamaBarang()%></div></td>
                                    <td bgcolor="#FFFFFF"><div align="center"><%=temp.getJenisBarang()%></div></td>
                                    <td bgcolor="#FFFFFF"><div align="center"><%=temp.getHargaBarang()%></div></td>
                                    <td bgcolor="#FFFFFF"><div align="center"><%=temp.getJumlahBarang()%></div></td>
                                    <td bgcolor="#FFFFFF"><div align="center"><%=temp.getId_pengiriman()%></div></td>
                                    <td bgcolor="#FFFFFF"><div align="center"><%=temp.getTgl_pengiriman()%></div></td>
                                </tr>
                                <%
                                        }
                                %>
                            </table>

                            </body>
                            </html>
